package www.xudan.android_ffmpeg;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;

import java.io.File;
import java.io.IOException;

import VideoHandle.EpDraw;
import VideoHandle.EpEditor;
import VideoHandle.EpVideo;
import VideoHandle.OnEditorListener;

import static www.xudan.android_ffmpeg.Constance.path;
import static www.xudan.android_ffmpeg.Constance.path_copy_gif;
import static www.xudan.android_ffmpeg.Constance.path_copy_img;
import static www.xudan.android_ffmpeg.Constance.path_copy_video;

public class ShowActivity extends AppCompatActivity implements View.OnClickListener {

    private VideoView mShow_vv;
    private Button mBtn_add_img;
    private Button mBtn_add_music;
    private Button mBtn_add_word;
    private Button mBtn_add_gif;
    private String mPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        mPath = getIntent().getStringExtra("path");
        mShow_vv = findViewById(R.id.show_vv);
        mBtn_add_img = findViewById(R.id.btn_add_img);
        mBtn_add_img.setOnClickListener(this);
        mBtn_add_music = findViewById(R.id.btn_add_music);
        mBtn_add_music.setOnClickListener(this);
        mBtn_add_word = findViewById(R.id.btn_add_word);
        mBtn_add_word.setOnClickListener(this);
        mBtn_add_gif = findViewById(R.id.btn_add_gif);
        mBtn_add_gif.setOnClickListener(this);

        initVideoView(mShow_vv, mPath);
    }

    private void initVideoView(final VideoView videoView, String path) {
        videoView.setVideoPath(path);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                videoView.start();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_add_img:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        addImage(path_copy_video, path_copy_img, path + "img.mp4", false);
                    }
                }).start();
                break;
            case R.id.btn_add_music:
                break;
            case R.id.btn_add_word:
                break;
            case R.id.btn_add_gif:
                addImage(path_copy_video, path_copy_gif, path + "gif.mp4", true);
                break;
        }
    }

    private void addImage(String video_path, String img_path, final String path_copy_img, boolean isGif) {
        File file = new File(path_copy_img);
        if (file.exists()) {
            file.delete();
        } else {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
        }
        EpVideo epVideo = new EpVideo(video_path);
        epVideo.addDraw(new EpDraw(img_path, 20, 20, 50.0f, 50.0f, isGif));
        EpEditor.OutputOption outputOption = new EpEditor.OutputOption(path_copy_img);
        outputOption.setHeight(1920);
        outputOption.setWidth(1280);
        outputOption.bitRate = 10;
        outputOption.frameRate = 30;
        EpEditor.exec(epVideo, outputOption, new OnEditorListener() {
            @Override
            public void onSuccess() {
                Log.i("xd---", "onSuccess");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initVideoView(mShow_vv, path_copy_img);
                    }
                });
            }

            @Override
            public void onFailure() {
                Log.i("xd---", "onFailure");

            }

            @Override
            public void onProgress(float progress) {
                Log.i("xd---", "onProgress===" + progress);

            }
        });
    }
}
